<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
    wrapClassName="ant-modal-cust-warp"
    style="top: 5%; height: 85%; overflow-y: hidden"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" v-bind="layout" :model="model">
        <a-form-model-item label="优惠券名称" required prop="title">
          <a-input v-model="model.title" placeholder="请输入优惠券名称" />
        </a-form-model-item>

        <a-form-model-item label="优惠券金额" required prop="money">
          <a-input v-model="model.money" placeholder="请输入优惠券金额" />
        </a-form-model-item>

        <a-form-model-item label="使用基地" required prop="shopId">
          <a-select v-model="model.shopId" placeholder="请选择基地" :getPopupContainer="(target) => target.parentNode">
            <a-select-option v-for="(item, index) in baseOptions" :key="index.toString()" :value="item.key">
              {{ item.value }}
            </a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="使用门槛" required prop="rule">
          <a-input-number :min="1" style="width: 100%" placeholder="请输入使用门槛金额"  v-model="model.rule" />
        </a-form-model-item>

        <a-form-model-item label="使用方法" required prop="ruleRemark">
          <a-input v-model="model.ruleRemark" placeholder="请输入使用方法" />
        </a-form-model-item>

        <a-form-model-item label="开始时间" >
          <a-date-picker
            style="width: 100%"
            placeholder="请选择优惠券开始时间"
            v-model="model.startDay"
            :format="dateFormat"
            :getCalendarContainer="(node) => node.parentNode"
          />
        </a-form-model-item>

        <a-form-model-item label="结束时间">
          <a-date-picker
            style="width: 100%"
            placeholder="请选择优惠券结束时间"
            v-model="model.endDay"
            :format="dateFormat"
            :getCalendarContainer="(node) => node.parentNode"
          />
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import { addCoupon, editCoupon } from '@/api/api'
import { getAction } from '@/api/manage'
export default {
  name: 'BusinessModal',
  data() {
    return {
      title: '操作',
      visible: false,
      baseOptions: [],
      disableSubmit: false,
      dateFormat: 'YYYY-MM-DD',
      model: {},
      layout: {
        labelCol: { span: 3 },
        wrapperCol: { span: 14 },
      },
      confirmLoading: false,
    }
  },
  created() {
      this.getBase();
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    getBase() {
      let url = '/wx/shop/getShopValue'
      getAction(url).then((res) => {
        if (res.code === 20000) {
          this.baseOptions = res.data
          console.log(this.baseOptions)
        }
      })
    },
    add() {
      this.visible = true
      this.edit(this.modelDefault)
    },
    edit(record) {
      console.log('record', record)
      this.model = Object.assign({}, this.defaultModel, record)
      this.visible = true
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      console.log('model', that.model)
      const params = this.model
      console.log('params', params)
      let obj
      if (!that.model.id) {
        obj = addCoupon(params)
      } else {
        obj = editCoupon(params)
      }
      obj.then((res) => {
        if (res.code === 20000) {
          that.$message.success('添加成功！')
          that.close()
          that.$emit('ok')
        } else {
          that.$message.warning('添加失败！')
          that.close()
        }
      })
    },
    handleCancel() {
      this.close()
    },
  },
}
</script>

<style scoped>
</style>